package x.ovo.wechat.bot.http.request.core;

import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import x.ovo.wechat.bot.common.Constant;
import x.ovo.wechat.bot.common.http.request.ApiRequest;
import x.ovo.wechat.bot.common.http.session.LoginSession;

import java.util.function.Function;

public class PushLoginRequest extends ApiRequest<String> {

    public PushLoginRequest(LoginSession session) {
        super(session);
    }

    @Override
    public String getUrl() {
        return UrlBuilder.of(this.session.getUrl())
                .addPath(Constant.PUSH_LOGIN)
                .addQuery("uin", this.session.getWxUin())
                .build();
    }

    @Override
    public Function<String, String> responseHandler() {
        return s -> {
            JSONObject object = JSONUtil.parseObj(s);
            return object.getInt("ret") == 0 ? object.getStr("uuid") : null;
        };
    }
}
